package org.poseidon.report.controller;

import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.poseidon.common.annotation.Log;
import org.poseidon.common.core.controller.BaseController;
import org.poseidon.common.core.domain.CommonResult;
import org.poseidon.common.core.page.TableDataInfo;
import org.poseidon.common.enums.BusinessType;
import org.poseidon.common.utils.poi.ExcelUtil;
import org.poseidon.report.converter.DiaryConverter;
import org.poseidon.report.domain.BuDiary;
import org.poseidon.report.domain.vo.BuDiaryVO;
import org.poseidon.report.input.BuDiaryInput;
import org.poseidon.report.service.IBuDiaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 周报Controller
 *
 * @author gfc
 * @date 2022-02-22
 */
@RestController
@RequestMapping("/report/diary")
@Api(tags = "周报")
public class BuDiaryController extends BaseController {
    @Autowired
    private IBuDiaryService buDiaryService;

    /**
     * 查询周报列表
     */
    @ApiOperation(value = "查询周报列表")
    @PreAuthorize("@ss.hasPermi('report:diary:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody BuDiaryInput input) {
        PageHelper.startPage(input.getPageNum(), input.getPageSize());
        List<BuDiaryVO> list = buDiaryService.selectiveList(DiaryConverter.convertInputToDiary(input));
        return getDataTable(list);
    }

    /**
     * 导出周报列表
     */
    @ApiOperation(value = "导出列表")
    @PreAuthorize("@ss.hasPermi('report:diary:export')")
    @Log(title = "周报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuDiaryVO buDiary) {
        List<BuDiaryVO> list = buDiaryService.selectBuDiaryList(buDiary);
        ExcelUtil<BuDiaryVO> util = new ExcelUtil<>(BuDiaryVO.class);
        util.exportExcel(response, list, "周报数据");
    }
    /**
     * 获取周报详细信息
     */
    @ApiOperation(value = "获取周报详细信息")
    @PreAuthorize("@ss.hasPermi('report:diary:query')")
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable("id") Integer id) {
        return CommonResult.success(buDiaryService.selectByPrimaryId(id));
    }

    /**
     * 新增周报
     */
    @ApiOperation(value = "新增周报")
    @PreAuthorize("@ss.hasPermi('report:diary:add')")
    @Log(title = "周报", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@RequestBody BuDiary buDiary) {
        return toAjax(buDiaryService.insertBuDiary(buDiary));
    }

    /**
     * 修改周报
     */
    @ApiOperation(value = "修改周报")
    @PreAuthorize("@ss.hasPermi('report:diary:edit')")
    @Log(title = "周报", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public CommonResult edit(@RequestBody BuDiary buDiary) {
        return toAjax(buDiaryService.updateBuDiary(buDiary));
    }

    /**
     * 删除周报
     */
    @ApiOperation(value = "删除周报")
    @PreAuthorize("@ss.hasPermi('report:diary:remove')")
    @Log(title = "周报", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteById")
    public CommonResult remove(@RequestBody List<Integer> ids) {
        return toAjax(buDiaryService.deleteBuDiaryByIds(ids));
    }
}
